/**
 * 基本信息标签页相关代码
 */
var zjs = (function() {

	var customerId = qmsk.getQueryValue("customerId");
	var memberCardNo = qmsk.getQueryValue("memberCardNo");
	var memberId = qmsk.getQueryValue("memberId");
	var $saveButton = $("<div class='BottomBtn'><input type='button' value='保存信息'></div>");

	var cache = {};

	function clearInput(id) {
		$("#" + id).find("input[type='text']").val('');
		$("#" + id).find("input[type='checkbox']").val('');
		$("#" + id).find("input[type='radio']").val('');
		$("#" + id).find("select").val('');
		$("#" + id).find("textarea").val('');
	}

	function disabledInput(id) {
		$("#" + id).find("input[type='text']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='checkbox']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='radio']").attr('disabled', 'disabled');
		$("#" + id).find("select").attr('disabled', 'disabled');
		$("#" + id).find("textarea").attr('disabled', 'disabled');
	}

	function removeDisabledInput(id) {
		$("#" + id).find("input[type='text']").removeAttr('disabled');
		$("#" + id).find("input[type='checkbox']").removeAttr('disabled');
		$("#" + id).find("input[type='radio']").removeAttr('disabled');
		$("#" + id).find("select").removeAttr('disabled');
		$("#" + id).find("textarea").removeAttr('disabled');
	}

	/*
	 * 页面加载的时候进行的初始化
	 */
	function loadInit() {
		disabledInput("zjs_bjsxx");
		if (qmsk.isEmpty(memberCardNo) || qmsk.isEmpty(memberId)) {
			return;
		}
		clearInput("zjs_bjsxx");

		$("#zjs_add").unbind().bind('click', function() {
			clearInput("zjs_bjsxx");
			removeDisabledInput("zjs_bjsxx");
			$saveButton.remove();
			$("#zjs_bjsxx").append($saveButton);
			$saveButton.unbind().bind('click', insert);
		});
		loadSimpleList();

	}

	function insert() {
		var data = collectionData();
		qmsk.post('../recommendMan/insert/' + memberCardNo, data, function() {
			artSuccess('添加成功');
		});
	}

	function collectionData() {
		var heads = ["recommendedName", "mp", "type", "memo", "status", "giveIntegral", "rebateAmount"];

		var ids = [];
		for (var i = 0; i < heads.length; i++) {
			ids[i] = 'zjs_' + heads[i];
		}
		var data = qmsk.collectionData(heads, ids);
		return data;
	}

	function loadSimpleList() {

		qmsk.get('../recommendMan/find/list/' + memberId + '/0/99', {
			customerId : customerId
		}, function(response) {
			cache.list = response;
			qmsk.dataTable({
				tbodyId : 'zjs_list',
				heads : ["createdTime","recommendedName", "mp", "type", "giveIntegral", "rebateAmount", "status", "id"],
				response : response,
				sup : ['zjs_list_sup', '共 [index] 条'],
				handler : [[3, typeHandler], [6, statusHandler], [7, detailButton]]
			});
		});

		var typeHandler = function(type, row) {
			if (type == 1)
				return '维修';
			if (type == 2)
				return '试驾';
			if (type == 3)
				return '保养';
			if (type == 4)
				return '保险';
			return '';
		};
		var statusHandler = function(sex, row) {
			if (sex == 0)
				return '正常';
			else
				return '非正常';
		};
		var detailButton = function(id, row) {
			return "<input onclick='_loadZjsDetail(" + row + ")' type='button' class='common_btn' value='查看详情'>";
		};

	}


	window._loadZjsDetail = loadDetail;

	function loadDetail(row) {
		$saveButton.remove();
		var response = cache.list;
		bindData(response, row);
		disabledInput("zjs_bjsxx");
		$("#zjs_bjsxx_edit").unbind().bind('click', function() {
			removeDisabledInput("zjs_bjsxx");
			$saveButton.remove();
			$("#zjs_bjsxx").append($saveButton);
			$saveButton.unbind().bind('click', update);
		});

	}

	function update() {
		var id = cache.id;
		var data = collectionData();
		qmsk.post('../recommendMan/update/' + id, data, function() {
			artSuccess('更新成功!');
			$saveButton.remove();
			disabledInput("zjs_bjsxx");
			loadSimpleList();
		});
	}

	function bindData(response, row) {
		var ids = [];
		var heads = ["id", "recommendedName", "mp", "type", "giveIntegral", "rebateAmount", "status", "memo"];
		var temp = response.getObject(heads, row);
		cache.id = temp.id;
		var data = {};
		for (var i = 0; i < heads.length; i++) {
			data['zjs_' + heads[i]] = temp[heads[i]];
		}
		qmsk.bindBean(data);
	}

	return {
		init : function() {
			loadInit();
		}
	};

})();
